import { defineStore } from "pinia";
import { storage } from "../utils/storage";
import profile from "../assets/images/profile.jpg";
export const useUsersStore = defineStore("users", {
  state: () => {
    return {
      name: storage.get("name"),
      avatar: storage.get("avatar"),
      roles: storage.get("roles"),
      permissions: storage.get("permissions"),
      userInfo: storage.get("userInfo"),
    };
  },
  actions: {
    // 缓存用户信息数据
    setUserInfo(obj) {
      const user = obj.user || {};
      const avatar = user.avatar
        ? import.meta.env.BASE_URL + user.avatar
        : profile;
      const username = user.userName || "";
      if (obj.roles && obj.roles.length > 0) {
        this.roles = obj.roles;
        storage.set("roles", obj.roles);
        this.permissions = obj.permissions;
        storage.set("permissions", obj.permissions);
      } else {
        this.roles = ["ROLE_DEFAULT"];
        storage.set("roles", ["ROLE_DEFAULT"]);
      }
      this.name = username;
      this.avatar = avatar;
      this.userInfo = user;
      storage.set("name", username);
      storage.set("avatar", avatar);
      storage.set("userInfo", user);
    },
  },
});
